package com.gxjc.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.gxjc.model.DataAndWaring;
import com.gxjc.model.Home;
import com.gxjc.model.Nature;
import com.gxjc.model.Node;
import com.gxjc.model.NodeData;
import com.gxjc.service.HomeService;
import com.gxjc.service.NatureService;
import com.gxjc.service.NodeDataService;
import com.gxjc.service.NodeService;
import com.opensymphony.xwork2.ActionSupport;

public class NodeDataAction extends ActionSupport {
	@Resource
	NodeService nodeService;
	@Resource
	HomeService homeService;
	@Resource
	NodeDataService nodeDataService;
	@Resource
	NatureService natureService;
	public String tojsp;
	public List<NodeData> list = new ArrayList<>();
	public List<Node> nodes = new ArrayList<>();
	public List<Home> homes = new ArrayList<>();
	public List<DataAndWaring> dataAndWaring = new ArrayList<>();
	public Integer homeId;
	public Integer nodeId;
	public String getAll(){
		list = nodeDataService.getAll();
		return "success";
	}
	public String preview(){
		nodes = nodeService.getAll();
		tojsp="addData.jsp";
		return SUCCESS;
	}
	/**
	 *  先保存Nature 和 NodeData 关联数据 然后通过更新NodeData 的而方法把Node 关联到NodeData 
		如果不这样做会报错，目前没有找到其他解决办法。
		错误信息a different object with the same identifier value was already associated with thesession时，
		一般是因为在hibernate中同一个session里面有了两个相同标识但是是不同实体。
		Node会报这个错
		http://lcy0202.iteye.com/blog/1453868 解释的很清楚
	 */
	public String add(){
		Date date = new Date();
		List<Nature> natures = natureService.getAll();
		for(int i = 0;i<natures.size();i++){
			NodeData nodeData = new NodeData();
			//dataAndWaring 是从addData.jsp中赋值的 为了保持时间相同 采用list一次把所有数据带过来
			//这个顺序要与Nature中的数据（字段名）顺序一样
			//后期可以对这个进行优化 采取一对一的方式
			nodeData.setData(dataAndWaring.get(i).getData());
			nodeData.setNature(natures.get(i));
			nodeData.setTime(date);
			nodeData.setWaring(dataAndWaring.get(i).getWaring());
			nodeDataService.save(nodeData);//先插入
			
			List<NodeData> nodeDatas = nodeDataService.getNodeDataBySql("from NodeData where id="+nodeData.getId());
			NodeData nodeData1 = nodeDatas.get(0); 
			Node n = nodeService.getNodeById(nodeId);
			nodeData1.setNode(n);	
			nodeDataService.update(nodeData1);//后更新
		}
		tojsp = "nodedata_preview";
		return "preview";
	}
	public String update(){
		List<NodeData> nodeDatas = nodeDataService.getNodeDataBySql("from NodeData where id=10");
		NodeData nodeData = nodeDatas.get(0); 
		Node n = nodeService.getNodeById(1);
		nodeData.setNode(n);
		nodeDataService.update(nodeData);
		tojsp="nodedata_preview";
		return SUCCESS;
	}
	public String informationView(){
		homes = homeService.getAll();
		if(homeId==null) setHomeId(homes.get(0).getId());
		nodes = nodeService.getNodeBySql("from Node as n where n.home.id="+getHomeId());
		if(nodeId==null){
			if(nodes!=null && nodes.size()>0){
				nodeId = nodes.get(0).getId();
			}else{
				nodeId=0;
			}
		}
		int id=nodeId;
		int limit = 6;//返回结果数 六十条Data 对应十条数据
		list = nodeDataService.getNodeDataBySql("from NodeData as n where n.node.id="+ id +" order by n.time desc,n.nature.id asc",limit);
		tojsp = "nowstatus.jsp";
		return SUCCESS;	
		/*int id = 1;
		if(nodeId!=null) id = nodeId;
		list = nodeDataService.getNodeDataBySql("");
		tojsp = "nowstatus.jsp";
		return SUCCESS;	*/
	}
	public String hestoryView(){
		homes = homeService.getAll();
		if(homeId==null) setHomeId(homes.get(0).getId());
		nodes = nodeService.getNodeBySql("from Node as n where n.home.id="+getHomeId());
		if(nodeId==null){
			if(nodes!=null && nodes.size()>0){
				nodeId = nodes.get(0).getId();
			}else{
				nodeId=0;
			}
		}
		int id=nodeId;
		int limit = 60;//返回结果数 六十条Data 对应十条数据
		list = nodeDataService.getNodeDataBySql("from NodeData as n where n.node.id="+ id +" order by n.time desc,n.nature.id asc",limit);
		tojsp = "hestory.jsp";
		return SUCCESS;	
	}
	public String getTojsp() {
		return tojsp;
	}
	public void setTojsp(String tojsp) {
		this.tojsp = tojsp;
	}
	public List<NodeData> getList() {
		return list;
	}
	public void setList(List<NodeData> list) {
		this.list = list;
	}
	public List<Node> getNodes() {
		return nodes;
	}
	public void setNodes(List<Node> nodes) {
		this.nodes = nodes;
	}
	public List<DataAndWaring> getDataAndWaring() {
		return dataAndWaring;
	}
	public void setDataAndWaring(List<DataAndWaring> dataAndWaring) {
		this.dataAndWaring = dataAndWaring;
	}
	public Integer getNodeId() {
		return nodeId;
	}
	public void setNodeId(Integer nodeId) {
		this.nodeId = nodeId;
	}
	public Integer getHomeId() {
		return homeId;
	}
	public void setHomeId(Integer homeId) {
		this.homeId = homeId;
	}
	
	
}
